package com.own.demo.geodemo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.utils.FileToStringUtils;
import com.own.demo.utils.GeoTools;
import com.own.demo.utils.RegionUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.locationtech.jts.geom.Coordinate;

import java.io.File;

public class 点转绑路调用返回解析 {
    public static void main(String[] args) {
        File geoZJ = new File("E:\\ownCode\\emg-geojson\\src\\test\\java\\com\\own\\demo\\geodemo\\点转绑路XML.txt");
        String geoData= FileToStringUtils.txt2String(geoZJ);
        JSONArray jsonArray= JSONObject.parseObject(geoData).getJSONObject("data").getJSONArray("points");
        //创建document对象
        Document document = DocumentHelper.createDocument();
        //添加节点
        Element gpx = document.addElement("gpx");
        Element trk=gpx.addElement("trk");
        Element trkseg=trk.addElement("trkseg");

        int row=jsonArray.size();
        Coordinate  threePoint1=null;
        Coordinate  threePoint2=null;
        Coordinate  threePoint3=null;
        int sum=0;
        for(int i=0;i<row;i++){
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            double lon=jsonObject.getDouble("lon");
            double lat=jsonObject.getDouble("lat");
            Element trkpt= trkseg.addElement("trkpt");
            trkpt.addAttribute("lon",lon+"");
            trkpt.addAttribute("lat",lat+"");
            threePoint1=new Coordinate(lon,lat);

            if(i+1<row-1) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i + 1);
                double lon2 = jsonObject2.getDouble("lon");
                double lat2 = jsonObject2.getDouble("lat");
                threePoint2 = new Coordinate(lon2, lat2);

                double distance= RegionUtil.getDistance(threePoint1.y,threePoint1.x,threePoint2.y,threePoint2.x);
                System.out.println(distance);


                if(distance>20){
                   sum= sum+1;
                }

            }
        }
        System.out.println("-->"+sum);
        System.out.println(row);
        System.out.println(document.asXML());
         geoZJ = new File("E:\\ownCode\\emg-geojson\\src\\test\\java\\com\\own\\demo\\geodemo\\点转绑路返回.json");
         geoData= FileToStringUtils.txt2String(geoZJ);

        jsonArray= JSONObject.parseObject(geoData).getJSONArray("paths");

        JSONArray jsonArray1= jsonArray.getJSONObject(0).getJSONObject("points").getJSONArray("coordinates");
        System.out.println("json-->"+jsonArray1.size());
        JSONArray inst=  jsonArray.getJSONObject(0).getJSONArray("instructions");
        System.out.println("json-->"+inst.size());
        System.out.println();

//        <trkpt lon="" lat=""/>
//        <trkpt lon="" lat=""/>
        System.out.println(RegionUtil.getDistance(39.20887,118.986786, 39.208935,118.98707));

    }
}
